Dreamweaver Article

How to Develop an Online Shopping Cart (Part Two)

Lawrence Cramer

www.cartweaver.com

This is part two of a three-part series on shopping carts. If you haven't already been through part one, you should do so before reading part two. In parts one and two, I give you the background theory and knowledge to understand how shopping carts work and what you need to know to build one. In part three, I will take you through actually building a simple shopping cart in ColdFusion using Dreamweaver MX 2004. You can see a hosted version of the shopping cart you will build in part three.

Business Logic, Database Design, Security and Payment Collection Options

In this tutorial, I am going to be talking about a few subjects that tend to at least make the average web designers eyes glaze over, and at worst strike terror into their hearts. But WE are not AVERAGE web designers now are we? We are e-commerce developers!

Note: Throughout this article, I will be using ColdFusion as the application server platform. Although there may be some variations on how to do things from platform to platform, the principles remain the same. I'll mention some of these differences as I go when they have a direct effect on structure, but for the most part, what I cover in parts one and two will apply to all application server platforms.

Business Logic

There's really nothing complicated about understanding the business logic behind a shopping cart application. It's all pretty basic. It's like a web store and cash register. You display your merchandise, sell stuff, and keep track of your customers and their orders. What is more involved is developing the business logic behind a cart application and then developing the database to fit, not only now, but into the future as well.

The important thing is sitting down and mapping out the business logic you will use. Ask yourself or your client questions like:

  • Who will have access to the data?
  • Will there be multiple levels of access?
  • How will you categorize the data?
  • How will you display the merchandise?
  • How will the customers search for or browse for the merchandise?
  • Once a product has orders associated with it, will you allow the product to be deleted?
  • Will you allow customer orders to be manually entered (via phone, fax, e-mail, mail, etc) or only come through the web site?
  • Once a customer has placed orders, will you allow the customer to be deleted from the database?
  • Will you create a status for active and inactive customers?
  • Will you keep track of stock?
  • Will you allow items that are out of stock show on the web, thus creating back orders?
  • How will you designate a new order from a shipped order?

As you can see, mapping out the business logic and planning the database is where you will spend a lot of your time. Your answers to these questions will not only guide the scope and structure of the database but the code and logic of the shopping cart portion of your web site.

Avoided Creating Dead Ends

About this time, you may say to yourself, "I'm a web developer, not a database programmer." Which may be true, but it's important to accept that planning the database is every bit as important, if not more so, than developing of the site itself.

If you are going to be developing e-commerce applications, you are at least going to have to become familiar with database design. Even if someone else develops the database, you're going to have to know how it works to effectively communicate with it.

Note: To help you ease into understanding and working with databases, I highly recommend you think about getting Database Design for Mere Mortals: A Hands-On Guide to Relational Database Design, Second Edition by Michael J. Hernandez. This book is an easy read and does an excellent job of covering database design essentials.

Take your time. Every hour you spend planning out the business logic of your application you'll get back ten-fold later in saved headaches and restful nights. But what if you don't have the time? If you need to, get some help. The business logic of your site and the resulting database design is important enough that if this is not your expertise and you don't have the time to dive into it, you should get some help.

The important thing is take the time to ask the questions and work out a number of scenarios to come up with a business logic and accompanying database design that will serve you now, and grow with you. As we stated in part one of this article series, you will have to live with your database design decisions for a long time. So save yourself a lot of possible frustration and before you even open Dreamweaver and write your first line of code. Take the time to plan your database.

Now, let's take a moment to look a how your application talks to the database.

SQL… Learn it, Love It!

SQL (Structured Query Language) is a database centric programming language. SQL is basically the way all application server languages communicate with databases. I won't go into depth about it here. Just know you'll have to take some time to at least understand SQL, if not know it.

Note: A fantastic little book on SQL is Sams Teach Yourself SQL in 10 Minutes (2nd Edition) by Ben Forta. It could possibly be the best $14.00 you'll ever spend. I use this great little reference so often that I took my copy down to the local office supply and had them cut the binding off of it and them spiral bind it so it would lay flat to the page I need. It's never far from reach.

With a better understanding of database design and SQL, and your database all mapped out, you're ready to move forward. For more information on database design, development and SQL, visit the Database Topic Center. Now there's another very important subject to consider: Security.

Security and Liability—Protect Your Customer's Data and Yourself

There are serious issues to consider when it comes to data security. Even capturing someone's credit card data temporarily is a risk. Storing it in your database is even a bigger one. The merchant and the developer have a responsibility to show "Due Diligence" in regard to security. Due to the increasing concern about identity theft, you must take necessary precautions to protect your customers' personal and credit information. Let's look at some security measures that your app must take into consideration. Here are a few points to consider.

  • Protect customer credit card data.
  • Secure your database.
  • Protect data in transit with an SSL (Secure Socket Layer) certificate An SSL encrypts your pages and accompanying data when your browser requests it. It will only be unencrypted and viewable by the same browser that requested it. If anyone along the way should intercept the data packets, the data will be unusable.

Secure Socket Layer (SSL)

First, Let's Look at SSLs. There are two primary options when it comes to setting up SSL security for your e–commerce website: Shared SSL and Individual SSL, Which Is Better? Let's take a look at the options and weigh the merits of each.

Some Internet Service Providers (ISP) will make a Shared SSL available to their customers. The ISP shares its SSL with you by placing your secure pages on its SSL protected server.

Advantages:

  • Cheaper and sometimes free.
  • For the most part, a shared SSL is as secure as an individual SSL.
  • Easy to set up—the ISP already has it set up. They just internally map part of your site through their SSL server.

Disadvantages:

  • You aren't listed as the SSL certificate owner. This can potentially shake consumer confidence and loose sales.
  • Cannot be transferred if you move to a different host.
  • Incompatibility with many shopping carts. Since shared SSL requires a transfer to or through a separate server, many session-based carts will not work.

Your other choice is to purchase our own individual SSL certificate. When you purchase your own SSL certificate, the issuing company generates a unique SSL code or key. You must send this code exactly as is to your ISP to be set up on your site. It is set up and mapped to your site's IP address.

Advantages:

  • The SSL is registered to your company. If a user checks the certificate, you appear as the owner. This bolsters consumer confidence.
  • If you change ISPs, you take your SSL with you.
  • The SSL is installed on your site. No server transfer or switching required. This avoids session problems.

Disadvantages:

  • Individual SSLs cost more. They are not nearly as costly as they used to be though. You can purchase an SSL for under US$100 a year.
  • More is involved in setting it up, although knowledgeable ISPs make the process quite painless.

Over the last year or so, the cost of acquiring your own SSL has become much more reasonable. It has become a much less involved process as well. With the added consumer confidence an individual SSL provides, it really makes this the wise choice.

Database Security

With an SSL certificate, now your data is safe while it's moving around. But, what about when it's in your database? Is it secret? Is it safe? Evil is looking for your data. Identity theft and malicious mischief are very wide spread on the web. I've talked about securing your data in–transit. Now I'll talk about making sure it's safe in your database.

The most secure data is data you don't have. Think carefully about what to store in your database. The less sensitive the data you have in your site's database about you, your business, and your customers, the better.

Should you store credit data in your database? For example, if you are using an on-line payment processor, do you really need to store your customer's credit card data? If you store it, you are responsible for what happens to it. Why not just let the payment processor worry about that? I talk about data processors later in the article. If you store it, you need to think about securing it not only from the web, but internally as well. Most credit card theft is done by employees of the company acquiring the credit card information. Take care to keep the data secure at all levels—internally and externally.

What about company accounting data? Once again, it would be better to store this in a database that has no online access. It's better to keep this separate from your online data and then just synch the two data sources. Now, I'll talk about securing your database by hiding it and making it hard to get to.

Basic Steps To Secure Your Database

There have been many books written on the subject of database security. There's no way I can cover it all here, so I will just touch on the basics for now. It will be worth your while to become very familiar with the security options available to your database of choice before your site goes live.

  • Put file databases, such as Microsoft Access, above the root of your site. If this option is not available, have your ISP set the permissions of a folder in your web site as non-browsable. This will prevent anyone from browsing to your database file and downloading it.

    Figure 1. Place file databases outsite the root of your website.

    Figure 1. Place file databases outside the root of your website.

  • Set permissions on your database and use usernames and passwords in your queries. Here is an example of a ColdFusion query that uses a username and password.

      <cfquery 
         name="RecordsetName"
         datasource="YourDSN"
         username="YourUsername" 
         password="YourPassword""  >
                 SELECT *
                 FROM TableName
      </cfquery>
        

    Doing this makes it much more difficult for someone to try to guess what your datasource access information is and attempt to query your database.

  • Don't pass actual database table or field names in query strings.

    http://www.yoursite.com/pagename.cfm?MyID=1234

    In this example, the actual database field name for MyID should not be MyID. If someone should figure out your data source name (DSN), there is no use giving them the field names too. If a malicious person can get a hold of your database column names, he can manipulate and even delete your database. If you are using ColdFusion as your application server platform, consider using the cfqueryparam tag to avoid SQL injection hacks. See Dave Watt's article on Securing Database Access Using the cfqueryparam Tag for more information on this.

  • Dynamically populate your Data Source Name (DSN). Populate your DSN in your Application.cfm (ColdFusion), Global.asa (ASP), or in and include (PHP). Then, call the DSN dynamically in your queries. While you're at it, you can also set your DSN username and password and call them dynamically as well. Here is an example of how to do this in a ColdFusion application.

    Set the variables in your Application.cfm.

        <cfset request.MyDSN="ActualDSN">
        <cfset request.MyUsername="ActualUsername">
        <cfset request.MyPassword="ActualMyPassword >
        

    Call the variables in your queries.

    <cfquery 
      name="RecordsetName"
      datasource="<cfoutput>#request.MyDSN#</cfoutput>"
      username="<cfoutput>#request.MyUsername#</cfoutput>"
      password=" cfoutput>#request.MyPassword#</cfoutput>" >
           SELECT *
           FROM TableName
    </cfquery>
        

    This has several advantages First of all, you are not including this confidential data and repeating it in every query throughout your application. If fact, you can even place the code that creates these variable in an include file or a custom tag that you store in a secure folder. Then, call the include or custom tag from the Application.cfm file.

    Secondly, this makes your site maintenance much easier because it allows you to globally change your DSN, username, and password in one easy place. If you feel your DSN has been compromised, you can change it in seconds.

These are just a few general tips about security. Again you should take the time to see what security features your database has and take advantage of them. Also, take time to review the content on the Security Developer Center, and Ray West's article, Writing Secure Dreamweaver MX Applications.

Now that the shopping cart is safe and secure, let's see about getting the money.

Processing Credit Cards Online

This can be a little frustrating, so I will break down the options available to you into their basic components and walk through them one by one. It's not as bad as it seems. First, I'll talk about what the options are, and then I will discus how to integrate them into your site. There are three basic components to online payment processing:

  • Merchant account
  • Payment gateway
  • Interface between your site and the gateway

Merchant Accounts

First, what is a merchant account? A merchant account is simply a business bank account that you set up to accept payment through credit cards. Almost any bank can open a merchant account, but it is very important to find one that is Internet savvy. An Internet friendly business account is not just a business bank account or credit card account. The kind of merchant bank I am talking about is one that is familiar with interfacing with Internet payment gateways. This is the interface between your site and your bank. I'll get to those in a minute. Be sure to check this out before setting up an account. Doing your homework and asking pointed questions can save you a great deal of frustration, time, and money.

Don't get trapped! What many merchant accounts won't tell you, unless you specifically ask, is they will only interface with their own proprietary gateway. This could lead to a lot of custom programming and trouble that you could avoid if you were to go with a more common third party solution. Rates vary from bank to bank—a lot! Again, do your homework. Often times it is less expensive to even have a different merchant bank for your website than for your business.

Okay, you're a web developer and this is up to your client right? True, but by discussing these simple facts with your client you can become very popular when your advice in this area saves your client a bunch of money. A happy client is a paying client.

Payment Gateways and Payment Processors

Really, there are only two choices. Companies offering some kind of Internet payment processing are springing up everywhere. It's like how many kinds of cereal can you find in the breakfast isle at the grocery store? But all of these choices can be boiled down to two basic types.

  • Real time/payment gateways–real-time results
  • Non-Real Time/payment processors–results later

There are more differences between these two than just the speed at which they communicate with your application—differences that will directly affect your application. Maybe even your sanity! (Just kidding, kind of.) Let's examine the differences.

Real-Time Payment Gateways—In-Line Processing or Immediate Transactions

Your true payment gateways process the credit card authorization in–line. The shopping cart application will process the page until it reaches the code that interacts with the gateway. It will then pause while it waits for a response from the gateway. Once it receives the response, the shopping cart application server will then continue to process the page.

Figure 2. Payment gateway process flow

Figure 2. Payment gateway process flow

Advantages:

  • Instant feedback to the customer
  • You can base page logic on transaction results
  • If the transaction is unsuccessful, you can give the user an opportunity to try again.
  • Code to integrate is usually less complex
  • Cheaper in the long run, for higher volume business.

Disadvantages:

  • More difficult to set up (Merchant Accounts, Gateway Vendors, Credit requirements)
  • More expensive to set up initially and may be more expensive overall for low volume businesses

The most popular real time payment gateways are Verisign PayFlow Pro and Authorize Net. Verisign sells their service directly, while Authorize Net sells through a network of resellers. Authorize Net tends to be cheaper to set up and charges a more reasonable percentage

Payment Processors—"We'll Take Care Of This & Get Back To You"

Payment processors work quite differently. When your application server comes to the code that interfaces with the payment processor, it sends the information off—usually in form variables, but sometimes as URL variables. After sending the data, the server finishes the page and doesn't wait for any response. The results of the transaction will have to be handled later.

Note: A word about sending cart information via URL variables: Don't do it! A URL variable is the most accessible and hackable bit of information passed on the web. This is not a place to put your pricing information, or worse yet, credit card data. If you're looking at a payment processor that requires you to send cart or customer data through URL variables, look elsewhere. You've been warned.

Was the credit card approved or not? We'll have to wait and see. Payment processors will send the results of the transaction back to you, either by e-mail or through an HTTP post to a designated page. Getting a response can take anywhere from a few seconds to a day or two.

Figure 3. Payment processor process flow

Figure 3. Payment processor process flow

Advantages:

  • Easier to set up, usually do not require a commercial merchant account.
  • Usually do not have credit restrictions or require a business license.
  • Very low cost or free to set up.

Disadvantages:

  • Transaction is not processed in real-time. Does not provide instant feedback.
  • Often require the customer to leave your site to complete the transaction.
  • Results of the transaction delayed from seconds to days.
  • Code to integrate is more complex. The results of the transaction must be retrieved manually or by HTTP post and you will have to update your order records.
  • If the customer's credit card is declined, the user will be informed after their shopping session is over and they may not return to try again, resulting in lost sales.

Considering their speed disadvantage, why would anyone choose to use a payment processor over a payment gateway? The simple reason is usually convenience and price.

A Word About Customer Service

For the most part, customer service afforded the web developer by the payment processors and gateway vendors is less than noteworthy. Unfortunately, this is pretty much norm. PayPal has over the last year made some real strides in this respect with their Developer's Network. And some of the Authorize Net resellers do a better than usual job of answering your questions. But as a developer, it mostly boils down to biting the bullet by spending the time and making the effort to learn how your system of choice works and how to integrate with it.

In part three, I will bring this all together by taking you through developing a small e-commerce application and seeing how it all fits together. You will build a small database, develop a cart, and an example of a secure administrative back end. Until then, sit down with a good cup a coffee and take some time to read the books and resources mentioned in this article. Enjoy!


About the author

Lawrence's day job is web application development. He also develops Dreamweaver Extensions, helps out on the Macromedia forums, teaches at the local Community College, speaks at web development conferences around the country, and writes articles on web development and e-commerce. For fun, he takes motorcycle road trips and occasionally catches a fish or two with his wife of 25+ years. Lawrence has three children—two daughters and one son. Two are grown and one is still in school. You can find him at lawrence@cartweaver.com.